package org.stephen.recommend.controller;


import org.stephen.recommend.entity.Article;
import org.stephen.recommend.enums.AcceptLanguage;
import org.stephen.recommend.enums.LanguageType;
import org.stephen.recommend.model.CmsArticleRequestPageWithBlackList;
import org.stephen.recommend.util.Utils;
import org.stephen.recommend.service.RecommendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: RecommendController
 * Description:
 * Date: 2020/12/8 20:55
 *
 * @author stephen.qiu
 * @version 1.0
 */


@RestController
@RequestMapping(value = "recommend")
public class RecommendController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(RecommendController.class);
    @Autowired
    RecommendService recommendService;

    @PostMapping(value = "viewArticle")
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.POST},
            origins = "*")
    public void viewArticle(@RequestParam("articleId") String articleId) {
        String userId =  request.getHeader("user_id");
        if (Utils.isEmpty(userId) && Utils.isEmpty(request.getHeader("User-Identifier"))) {
            return;
        }
        String language = request.getHeader("Accept-Language");
        LanguageType languageType = AcceptLanguage.CN_LANGUAGE.equals(language) ? LanguageType.ZH_CN : LanguageType.EN_US;
        if (Utils.isNotEmpty(userId)) {
            logger.info("推荐系统开始,更新浏览记录 用户id" + userId);
        } else {
            userId = request.getHeader("User-Identifier");
            logger.info("推荐系统开始,更新浏览记录 用户设备码:" + userId);
        }
        recommendService.viewArticle(userId, articleId, languageType);
    }

    @PostMapping("/recommendArticles")
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.POST},
            origins = "*")
    public List<Article> recommendArticles(@RequestBody CmsArticleRequestPageWithBlackList requestPage) {
        String language = request.getHeader("Accept-Language");
        String userId =  request.getHeader("user_id");
        List<Article> articleList;
        LanguageType languageType = AcceptLanguage.CN_LANGUAGE.equals(language) ? LanguageType.ZH_CN : LanguageType.EN_US;
        if (Utils.isNotEmpty(userId)) {
            logger.info("推荐系统开始,推荐文章 用户id:" + userId);
        } else {
            userId = request.getHeader("User-Identifier");
            logger.info("推荐系统开始,推荐文章 当前无登录用户,获取用户设备标识:" + userId);
        }
        articleList = recommendService.recommendArticles(requestPage.getBlockArticleIds(), userId, languageType);
        return articleList;
    }

    @PostMapping("/deleteArticleById")
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.POST},
            origins = "*")
    public void deleteArticleById(@RequestParam(value = "articleId") String articleId) {
        if (Utils.isEmpty(articleId)) {
            return;
        }
        String language = request.getHeader("Accept-Language");
        LanguageType languageType = AcceptLanguage.CN_LANGUAGE.equals(language) ? LanguageType.ZH_CN : LanguageType.EN_US;
        logger.info("推荐系统开始,删除推荐文章 文章id:" + articleId);
        recommendService.deleteArticleById(articleId, languageType);
    }
}
